package view;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import utils.ScreenAdjustment;

//@author A0097788Y
public class BarChart extends JFrame{
	private String _title;
	private DefaultCategoryDataset _dataset = new DefaultCategoryDataset();
	private String _xLabel;
	private String _yLabel="SGD";

	/**
	 * Constructor
	 * @param applicationTitle, title of the pop-up window, String
	 * @param chartTitle, title of the chart, String
	 * @Precondition 
	 * @Postcondition sets both titles
	 */
	public BarChart(String applicationTitle, String chartTitle) {
		super(applicationTitle);
		_title = chartTitle;
	}

	/**
	 * @param title, String
	 * @Precondition
	 * @Postcondition sets chart title
	 */
	public void setTitle(String title){
		_title = title;
	}

	/**
	 * @return _title, String
	 * @Precondition
	 * @Postcondition returns chart title
	 */
	public String getTitle(){
		return _title;
	}

	/**
	 * @Precondition 
	 * @Postcondition Clears dataset 
	 */
	public void clear(){
		_dataset.clear();
	}

	/**
	 * Add dataset
	 * @param amount, double
	 * @param label, String
	 * @param group, String
	 * @Precondition amount is positive and has up to 2 decimal places
	 * @Postcondition data is added to dataset
	 */
	public void add(double amount, String label, String group){
		_dataset.addValue(amount, label, group);
	}

	/**
	 * Create chart based on data added to the dataset
	 * @Precondition 
	 * @Postcondition opens window with bar chart
	 */
	public void create(){
		final JFreeChart chart = createChart(_dataset);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(750, 450));
		setContentPane(chartPanel);
		setDefaultCloseOperation(HIDE_ON_CLOSE);  
		this.pack();
		this.setBounds(ScreenAdjustment.getAlignCenterX(750),ScreenAdjustment.getAlignCenterY(450), 750, 450);
		this.setVisible(true);
	}

	/**
	 * Plots the chart
	 * @param dataset, CategoryDataset
	 * @return chart, JFreeChart
	 * @Precondition 
	 * @Postcondition returns plotted chart 
	 */
	private JFreeChart createChart(final CategoryDataset dataset) {

		// create the chart...
		final JFreeChart chart = ChartFactory.createBarChart(
				_title,         			// chart title
				_xLabel,               		// domain axis label
				_yLabel,               	   	// range axis label
				dataset,                	// data
				PlotOrientation.VERTICAL, 	// orientation
				true,                     	// include legend
				true,                     	// tooltips
				false                     	// URLs?
				);

		// set the background color for the chart...
		chart.setBackgroundPaint(Color.white);

		//add slant to labels
		final CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();
		domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 12.0));

		return chart;
	}
}
